package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.canteens.dao.SmallCanteenPurchasemxDao;
import io.renren.modules.canteens.dto.*;
import io.renren.modules.canteens.entity.SmallCanteenPurchasemxEntity;
import io.renren.modules.canteens.service.*;
import io.renren.modules.sys.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小食堂采购明细表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class SmallCanteenPurchasemxServiceImpl extends CrudServiceImpl<SmallCanteenPurchasemxDao, SmallCanteenPurchasemxEntity, SmallCanteenPurchasemxDTO> implements SmallCanteenPurchasemxService {
    @Autowired
    private FoodService foodService;
    @Autowired
    private SmallCanteenPurchaseService smallCanteenPurchaseService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SmallCanteenPurchasemxDao smallCanteenPurchasemxDao;
    @Autowired
    private SupplierService supplierService;
    @Override
    public QueryWrapper<SmallCanteenPurchasemxEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SmallCanteenPurchasemxEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public Map getOrderMx(Map<String, Object> params) {
        String smallCanteenPurchaseId =  (String)params.get("smallCanteenPurchaseId");
        Map map = new HashMap<>();
        List<Map> list=new ArrayList<>();
        if(StringUtils.isNotBlank(smallCanteenPurchaseId)) {
            long stockOrderIdl = Long.parseLong(smallCanteenPurchaseId);
            List<SmallCanteenPurchasemxDTO> stockOrdermxList = smallCanteenPurchasemxDao.getsupplierOrder(stockOrderIdl);
            if (stockOrdermxList.size() > 0) {
                for (SmallCanteenPurchasemxDTO smallCanteenPurchasemxDTO : stockOrdermxList) {
                    Map map1 = new HashMap<>();
                    SupplierDTO supplierDTO = supplierService.get(smallCanteenPurchasemxDTO.getSupplierId());
                    map1.put("SupplierName", supplierDTO.getSupplierName());
                    map1.put("orderMx", smallCanteenPurchasemxDao.getOrderMx(stockOrderIdl, smallCanteenPurchasemxDTO.getSupplierId()));
                    map1.put("totalPrice", smallCanteenPurchasemxDao.getTotalPrice(stockOrderIdl, smallCanteenPurchasemxDTO.getSupplierId()).getTotalPrice());
                    list.add(map1);
                }
                map.put("list", list);
                map.put("totalMony", smallCanteenPurchasemxDao.getTotalMony(stockOrderIdl).getTotalPrice());


            }
        }
        return map;
    }

    @Override
    public List<SmallCanteenPurchasemxDTO> getOrderMx1(Map<String, Object> params) {
        String stockOrderId1 =  (String)params.get("stockOrderId");
        List<SmallCanteenPurchasemxDTO> list = new ArrayList<>();
        if(StringUtils.isNotBlank(stockOrderId1)){
            long stockOrderId = Long.parseLong(stockOrderId1);
            SmallCanteenPurchaseDTO smallCanteenPurchaseDTO = smallCanteenPurchaseService.get(stockOrderId);
            List<SmallCanteenPurchasemxDTO> stockOrdermxDTOList = smallCanteenPurchasemxDao.getsupplierOrder(stockOrderId);
            for (SmallCanteenPurchasemxDTO ordermxDTO:stockOrdermxDTOList) {
                List<SmallCanteenPurchasemxDTO> orderMx = smallCanteenPurchasemxDao.getOrderMx(stockOrderId, ordermxDTO.getSupplierId());
                for (SmallCanteenPurchasemxDTO ordermxDTO1:orderMx) {
                    ordermxDTO1.setTotalPrice1(String.valueOf(ordermxDTO1.getTotalPrice()));
                    list.add(ordermxDTO1);
                }
                SmallCanteenPurchasemxDTO totalPrice = smallCanteenPurchasemxDao.getTotalPrice(stockOrderId, ordermxDTO.getSupplierId());
                SmallCanteenPurchasemxDTO ordermxDTO1 = new SmallCanteenPurchasemxDTO();
                ordermxDTO1.setMealTime(smallCanteenPurchaseDTO.getMealTime());
                ordermxDTO1.setTotalPrice1("合计 ： "+ totalPrice.getTotalPrice());
                list.add(ordermxDTO1);
            }
            SmallCanteenPurchasemxDTO totalPrice = smallCanteenPurchasemxDao.getTotalMony(smallCanteenPurchaseDTO.getSmallCanteenPurchaseId());
            SmallCanteenPurchasemxDTO ordermxDTO = new SmallCanteenPurchasemxDTO();
            ordermxDTO.setMealTime(smallCanteenPurchaseDTO.getMealTime());
            ordermxDTO.setTotalPrice1("共计 ： "+totalPrice.getTotalPrice());
            list.add(ordermxDTO);

            String name = sysDeptService.get(smallCanteenPurchaseDTO.getCanteenId()).getName();
            for (SmallCanteenPurchasemxDTO mxDTO:list) {
                if(StringUtils.isNotBlank(mxDTO.getSupplierName())){
                    BigDecimal unitPrice = new BigDecimal(mxDTO.getUnitPrice());
                    BigDecimal rrivalNum = new BigDecimal(mxDTO.getArrivalNum());
                    DecimalFormat df2 =new DecimalFormat("#.00");
                    mxDTO.setTotalPrice1(df2.format(unitPrice.multiply(rrivalNum)));
                    mxDTO.setCanteenName(name+"每日进货清单");
                }
            }
        }
        return list;
    }
}